using Microsoft.AspNetCore.Mvc;
using MediPlatform.Write.Api.Models;
using MediPlatform.Write.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Write.Api.Controllers
{
    /// <summary>
    /// 退款申请写入控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RefundApplicationWriteController : ControllerBase
    {
        private readonly RefundApplicationWriteService _refundApplicationWriteService;
        private readonly ILogger<RefundApplicationWriteController> _logger;

        public RefundApplicationWriteController(
            RefundApplicationWriteService refundApplicationWriteService,
            ILogger<RefundApplicationWriteController> logger)
        {
            _refundApplicationWriteService = refundApplicationWriteService;
            _logger = logger;
        }

        /// <summary>
        /// 确认退款申请
        /// </summary>
        /// <param name="request">确认退款请求</param>
        /// <returns>处理结果</returns>
        [HttpPost("confirm")]
        public async Task<ApiResult<object>> ConfirmRefund([FromBody] ConfirmRefundRequest request)
        {
            try
            {
                _logger.LogInformation("确认退款申请，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);

                // 验证请求参数
                if (request.Id <= 0)
                {
                    return ApiResult<object>.Error("退款申请ID无效");
                }

                if (request.OperatorId <= 0)
                {
                    return ApiResult<object>.Error("操作员ID无效");
                }

                // 调用服务处理
                var result = await _refundApplicationWriteService.ConfirmRefundAsync(request);

                if (result)
                {
                    return ApiResult<object>.Success("退款申请确认成功");
                }
                else
                {
                    return ApiResult<object>.Error("退款申请确认失败，请检查申请状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "确认退款申请失败，ID：{Id}", request.Id);
                return ApiResult<object>.Error("确认退款申请失败");
            }
        }

        /// <summary>
        /// 拒绝退款申请
        /// </summary>
        /// <param name="request">拒绝退款请求</param>
        /// <returns>处理结果</returns>
        [HttpPost("reject")]
        public async Task<ApiResult<object>> RejectRefund([FromBody] RejectRefundRequest request)
        {
            try
            {
                _logger.LogInformation("拒绝退款申请，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);

                // 验证请求参数
                if (request.Id <= 0)
                {
                    return ApiResult<object>.Error("退款申请ID无效");
                }

                if (request.OperatorId <= 0)
                {
                    return ApiResult<object>.Error("操作员ID无效");
                }

                // 调用服务处理
                var result = await _refundApplicationWriteService.RejectRefundAsync(request);

                if (result)
                {
                    return ApiResult<object>.Success("退款申请拒绝成功");
                }
                else
                {
                    return ApiResult<object>.Error("退款申请拒绝失败，请检查申请状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "拒绝退款申请失败，ID：{Id}", request.Id);
                return ApiResult<object>.Error("拒绝退款申请失败");
            }
        }
    }
} 